<?php
/**
 * 控制器基类
 * @author Jerry
 */
abstract class Controller
{
	public $controller;
	
	public $action ;
	
	public $param;
	
	public $params;
	
	public $model;
	
	/**
	 * 所有已实例化的Model数组
	 *
	 * @var array
	 */
	protected $models = null;
	
	public function __construct()
	{
		
	}
	
	public function setController($_ctl)
	{
		$this->controller = $_ctl;
		$this->model = $this->getModel($_ctl);
	}
	
	public function setAction($_act)
	{
		$this->action = $_act;
	}
	
	public function setParam($_param)
	{
		$this->param = $_param;
	}
	
	public function setParams($_params)
	{
		$this->params = $_params;
	}
	
	public function getModel($_model)
	{
		$model_name  = is_null($_model) ? $this->_controller : $_model;
		
		// 检查Model池中是否已经有这个Model
		if (isset($this->models[$model_name]))
		{
			return $this->models[$model_name];
		}
		
		// 拼接Model文件名
		$model_class = ucfirst(strtolower($model_name . 'Model'));
		$model_file = APP_DIR .'model' .DS. strtolower($model_name) . '.php';
		
		// 模型类文件如果存在，就实例化
		if (file_exists($model_file))
		{
			include_once($model_file);
			// 取得模型实例
			$model = new $model_class();
			// 将Model放入Model池
			$this->models[] = array('name' => $model_name, 'obj' => $model);
			
			return $model;
		}
		return NULL;
	}
	
	public function request($ctl, $act, $params = array())
	{
		$dispatcher = new Dispatcher();
		$ctl = $dispatcher->generateCtl($ctl);
		
		Loader::controller($ctl);
		$controller = new $ctl();
		$controller->setController($ctl);
		
		if (!method_exists($controller, $act))
		{
			throw new Exception("控制器{$ctl}中不存在方法{$act}");
		}
		
		return call_user_func_array(array($controller, $act), $params);
	}
	
	public function forward($ctl, $act, $params = array())
	{
		$dispatcher = new Dispatcher();
		$dispatcher->dispatch($ctl, $act, $params);
	}
	
	public function getPost($key = null, $default = null)
	{
		if (null === $key) {
			return $_POST;
		}
		
		return Arr::get($_POST, $key, $default);
	}
	
	public function getQuery($key = null, $default = null)
	{
		if (null === $key) {
			return $_GET;
		}
		
		return Arr::get($_GET, $key, $default);
	}
	
	public function responseAjax($success = '0', $message = '', $ext_array = array())
	{
		$data = array('success' => $success, 'message' => $message);
		if (is_array($ext_array))
		{
			$data = array_merge($data, $ext_array);
		}
		exit(json_encode($data));
	}
	
	public function response()
	{
		
	}
	
	function feedback($msg = '' ,$url = '')
	{
		$message = !empty($msg) ? $msg : '操作成功！';
		$url_forward = $url ? $url : '/';
		include(template('message', 'default'));
		exit(0);
	}
	
	public function getCookie($key = null, $default = null)
	{
		if (null === $key) {
			return $_COOKIE;
		}

		return Arr::get($_COOKIE, $key, $default);
	}
	
	public function getSession($key = null, $default = null)
	{
		if (null === $key) {
			return $_SESSION;
		}
		
		return Arr::get($_SESSION, $key, $default);
	}

	public function fetch()
	{
	
	}
	
	public function render()
	{
		return include(template($this->action, $this->controller));
	}
	
	public function css()
	{
		
	}
	
	public function js()
	{
		
	}
	
	/**
	 * 记录日志
	 *
	 * @param string $content
	 */
	public function log($content)
	{
		$content = date('Y-m-d H:i:s') . " " . $this->_controller .'->'. $this->_action . " : " . $content . "\n";
		$log_file_path = TEMP_DIR .DS. 'logs' .DS. 'controller.log';
		$handle = fopen($log_file_path, 'at');
		fwrite($handle, $content);
		fclose($handle);
	}
	
	public function logBegin()
	{
		ob_start();
	}
	
	public function logEnd()
	{
		$this->log(ob_get_contents());
		ob_end_clean();
	}
}
?>